//
//  ZOEPickerHeaderView.m
//  BasicModule
//
//  Created by 徐秀滨 on 2018/3/16.
//  Copyright © 2018年 Xiamen Zoenet Tech Co.,Ltd. All rights reserved.
//

#import "ZOEPickerHeaderView.h"

@interface ZOEPickerHeaderView()

@property (nonatomic, strong) UIToolbar *toolbar;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation ZOEPickerHeaderView

- (instancetype)init {
    self = [super initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 49)];
    if(self){
        [self setUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){
        [self setUI];
    }
    return self;
}

- (void)setUI {
    [self addSubview:self.toolbar];
    [self.toolbar addSubview:self.titleLabel];
}

- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
}

#pragma mark - Action

- (void)cancel {
    if([self.delegate respondsToSelector:@selector(cancel)]){
        [self.delegate cancel];
    }
}

- (void)confirm {
    if([self.delegate respondsToSelector:@selector(confirm)]){
        [self.delegate confirm];
    }
}

#pragma mark - Property

- (UIToolbar *)toolbar {
    if(!_toolbar){
        _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 49)];
        
        UIBarButtonItem *lefttem = [[UIBarButtonItem alloc] initWithCustomView:self.leftBtn];
        UIBarButtonItem *centerSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem *righttem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
        _toolbar.barTintColor = ToolBarBackgroundColor;
        _toolbar.items = @[lefttem,centerSpace,righttem];
    }
    return _toolbar;
}

- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.frame = CGRectMake(0, 0, 100, 49);
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_leftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_leftBtn setNeedsLayout];
        [_leftBtn layoutIfNeeded];
        CGFloat offset = _leftBtn.titleLabel.frame.origin.x - 15;
        _leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -offset, 0, offset);
        [_leftBtn setTitleColor:ToolBarTextColor forState:UIControlStateNormal];
        [_leftBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(0, 0, 100, 49);
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_rightBtn setTitle:@"确认" forState:UIControlStateNormal];
        [_rightBtn setNeedsLayout];
        [_rightBtn layoutIfNeeded];
        CGFloat offset = _leftBtn.frame.size.width - CGRectGetMaxX(_leftBtn.titleLabel.frame) - 15;
        _rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, offset, 0, -offset);
        [_rightBtn setTitleColor:ToolBarTextColor forState:UIControlStateNormal];
        [_rightBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

- (UILabel*)titleLabel {
    if(!_titleLabel){
        CGFloat width = self.toolbar.frame.size.width - self.rightBtn.frame.size.width * 2;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.toolbar.frame.size.width - width) / 2, 0, width, self.toolbar.frame.size.height)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

@end
